#include <string.h>
#include <pthread.h>
#include <tty/serial.h>


#define MTU     17


serial com4, com5, com6;
static int startRecv4 = 0;
static int startRecv5 = 0;
static int startRecv6 = 0;
int j4 = 0;
int j5 = 0;
int j6 = 0;

void* com_recv4(void *)
{
#if 1
    unsigned char *ptr;
    int recv = 0;
    int ret;
    //while(1) {
        while(1) {
            unsigned char rbuffer[256];
            ptr = &rbuffer[0];
            recv = 0;
            memset(ptr, 0, 256);
          again:
            ret = com4.recvdata(ptr, 1);
            if (ret < 0) {
                perror("com4 recvdata");
                printf("ret %d\n", ret);
                startRecv4 = 0;
                break;
            }
            recv += ret;
            printf("ret = %d, recv = %d v %d\n", ret, recv, (*ptr)+1);
            ptr += ret;
            if (recv < 256)
                goto again;
            printf("com4 read a frame\n");
            for(int i = 0; i < 256; i++) {
                if (i == rbuffer[i])
                    j4 ++;
                printf("rbuffer[%d] = %d\n", i, rbuffer[i]);
            }
            startRecv4 = 0;
            break;
        }
        //usleep(10000);
    //}
#endif
    return NULL;
}

void* com_recv5(void *)
{
    char *ptr;
    int recv = 0;
    int ret;
    while(1) {
        while(startRecv5) {
            char rbuffer[256];
            ptr = &rbuffer[0];
          again:
            ret = com5.recvdata(ptr, 256);
            if (ret < 0) {
                perror("com5 recvdata");
                printf("ret %d\n", ret);
                startRecv5 = 0;
                break;
            }
            recv += ret;
            ptr += ret;
            if (recv < 256)
                goto again;
            printf("com5 read a frame\n");
            for(int i = 0; i < 256; i++) {
                if (i == rbuffer[i])
                    j5 ++;
                printf("rbuffer[%d] = %x\n", i, rbuffer[i]);
            }
            startRecv5 = 0;
            break;
        }
        usleep(10000);
    }
    return NULL;
}

void* com_recv6(void *)
{
    char *ptr;
    int recv = 0;
    int ret;
    while(1) {
        while(startRecv6) {
            char rbuffer[256];
            ptr = &rbuffer[0];
          again:
            ret = com6.recvdata(ptr, 256);
            if (ret < 0) {
                perror("com5 recvdata");
                printf("ret %d\n", ret);
                startRecv6 = 0;
                break;
            }
            recv += ret;
            ptr += ret;
            if (recv < 256)
                goto again;
            printf("com6 read a frame\n");
            for(int i = 0; i < 256; i++) {
                if (i == rbuffer[i])
                    j6 ++;
                printf("rbuffer[%d] = %x\n", i, rbuffer[i]);
            }
            startRecv6 = 0;
            break;
        }
        usleep(10000);
    }
    return NULL;
}

int main(void)
{
    pthread_t pid_recv4, pid_recv5, pid_recv6;

    /* */
    if (com4.opendev("/dev/ttyUSB0")) {
        perror("com4 open");
        return -1;
    }
    if (com5.opendev("/dev/ttyUSB1")) {
        perror("com5 open");
        return -1;
    }
    if (com6.opendev("/dev/ttyUSB2")) {
        perror("com6 open");
        return -1;
    }

    com4.set_baudrate(B115200);
    com5.set_baudrate(B115200);

    /* */
    pthread_create(&pid_recv4, NULL, com_recv4, NULL);
    //pthread_create(&pid_recv5, NULL, com_recv5, NULL);
    //pthread_create(&pid_recv6, NULL, com_recv6, NULL);

    /* */
    {
        char sbuffer[256];
        char *ptr = &sbuffer[0];
        for(int i = 0; i < 256; i++)
            sbuffer[i] = i;
        for (int i = 0; i < 256/MTU; i++) {
            if(com5.senddata(ptr, MTU) != MTU) {
                perror("com5 write");
                return -1;
            };
            ptr += MTU;
            usleep(10000);
        }
#if 0
        if(com5.senddata(ptr, 16) != 16) {
            perror("com5 write");
            return -1;
        };
#endif
        while(1) usleep(100000);
    }

    return 0;
}
